package com.alili.springbooteditor.controller;

import com.alili.springbooteditor.entities.Persons;
import com.alili.springbooteditor.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class loginController {
    @Autowired
    LoginService loginService;


    @PostMapping("/user/login")
    public String login(Persons persons,  String username,  String password, HttpSession httpSession, Map<String,Object> map){
        persons.setUsername(username);
        persons.setPassword(password);
        Boolean T=loginService.checkLogin(persons);
        if (T){
            httpSession.setAttribute("loginUser",username);
            return "redirect:/main";
        }
        else {
            map.put("msg","用户名密码错误");
            return "login";
        }

    }
    @PostMapping("/visitor/login")
    public String visitorlogin(HttpSession httpSession){
        httpSession.setAttribute("loginUser","visitor");
        return "redirect:/index";
    }
}
